﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WinformCommonDemo.DataModel;

namespace WinformCommonDemo.CommControlOperate
{
    public class DataGridViewHelper
    {
        /// <summary>
        /// 表结构初始化
        /// </summary>
        /// <param name="dataGridView">控件名称</param>
        /// <param name="ColumnModel">结构模型集合</param>
        public static void InitDataGridView(DataGridView dataGridView, List<DataGridViewModel> ColumnModel)
        {
            dataGridView.AutoGenerateColumns = false;               //禁止自动创建列
            dataGridView.AllowUserToAddRows = false;                //禁止自动添加行
            dataGridView.AllowUserToOrderColumns = false;           //禁止排序
            dataGridView.AllowUserToResizeColumns = false;          //禁止列宽大小变化
            dataGridView.AllowUserToResizeRows = false;             //禁止行高大小变化
            dataGridView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize;//表头高度自适应
            dataGridView.RowHeadersVisible = false;                 //隐藏表空白首列
            dataGridView.Columns.Clear();
            dataGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect;   //是选中模式为单行整行选中
            dataGridView.BackgroundColor = dataGridView.Parent.BackColor;           //设置表格背景颜色与父容器颜色一致
            for (int i = 0; i < ColumnModel.Count; i++)
            {
                DataGridViewColumn fColumn;
                switch (ColumnModel[i].ColumnType)
                {
                    case ColumnType.Link:
                        fColumn = new DataGridViewColumn(new DataGridViewLinkCell());
                        break;
                    case ColumnType.Button:
                        fColumn = new DataGridViewColumn(new DataGridViewButtonCell());
                        break;
                    case ColumnType.Image:
                        fColumn = new DataGridViewColumn(new DataGridViewImageCell());
                        break;
                    case ColumnType.Combox:
                        fColumn = new DataGridViewColumn(new DataGridViewComboBoxCell());
                        break;
                    case ColumnType.CheckBox:
                        fColumn = new DataGridViewColumn(new DataGridViewCheckBoxCell());
                        break;
                    default:
                        fColumn = new DataGridViewColumn(new DataGridViewTextBoxCell());
                        break;
                }
                fColumn.HeaderText = ColumnModel[i].DisplayName;
                fColumn.DataPropertyName = ColumnModel[i].PropertyName;
                fColumn.Width = ColumnModel[i].Width;
                fColumn.ReadOnly = ColumnModel[i].ReadOnly;
                fColumn.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
                //如果该单元格支持编辑修改，让背景色与其他单元格有区分
                if (!ColumnModel[i].ReadOnly)
                {
                    fColumn.CellTemplate.Style.BackColor = Color.DarkGray;
                }

                // 这里改变 Add 顺序将会决定列显示顺序
                dataGridView.Columns.Add(fColumn);
            }
            // 设置所有列的标题行居中对齐
            foreach (DataGridViewColumn column in dataGridView.Columns)
            {
                column.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
            }
        }
        /// <summary>
        /// 数据绑定
        /// </summary>
        /// <typeparam name="T">表格结构泛型类</typeparam>
        /// <param name="dataGridView">表格控件对象</param>
        /// <param name="gridViewList">数据集合</param>
        public static void BindDataGridView<T>(DataGridView dataGridView, List<T> gridViewList)
        {
            if (gridViewList.Count > 0)
            {
                dataGridView.DataSource = gridViewList;
                dataGridView.Height = dataGridView.Rows.Count * dataGridView.RowTemplate.Height + dataGridView.ColumnHeadersHeight;
            }
            else
            {
                dataGridView.DataSource = gridViewList;
                dataGridView.Height = 60;
            }
        }
        /// <summary>
        /// 删除表格中的一行数据
        /// </summary>
        /// <typeparam name="T">表格结构泛型类</typeparam>
        /// <param name="dataGridView">表格控件对象</param>
        public static void DelSingleDataGridView<T>(DataGridView dataGridView)
        {
            List<T> list = new List<T>();
            // 检查是否有当前选中的单元格
            if (dataGridView.CurrentCell != null)
            {
                // 获取当前选中单元格所在的行
                DataGridViewRow currentRow = dataGridView.CurrentCell.OwningRow;
                foreach (DataGridViewRow row in dataGridView.Rows)
                {
                    if (row.Index != currentRow.Index)
                    {
                        T dataModel = (T)row.DataBoundItem;
                        list.Add(dataModel);
                    }
                }

                dataGridView.DataSource = list;
            }
        }
        /// <summary>
        /// 保存表格数据
        /// </summary>
        /// <typeparam name="T">表格结构泛型类</typeparam>
        /// <param name="dataGridView">表格控件对象</param>
        /// <returns></returns>
        public static List<T> SaveDataGridView<T>(DataGridView dataGridView)
        {
            List<T> list = new List<T>();
            if (dataGridView.Rows.Count > 0)
            {
                foreach (DataGridViewRow row in dataGridView.Rows)
                {
                    T dataModel = (T)row.DataBoundItem;
                    list.Add(dataModel);
                }
            }
            return list;
        }

        /// <summary>
        /// 清空表结构
        /// </summary>
        /// <param name="dataGridView"></param>
        public static void ClearDataGridView(DataGridView dataGridView)
        {
            dataGridView.DataSource = null;
        }
    }
}
